﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YArchitech.HVAC
{
	public partial class TerminalParameterForm : System.Windows.Forms.Form
	{
		public TerminalParameterForm(AirTerminalParamNew airTerminalPara)
		{
			this.InitializeComponent();
			//base.Icon = Resource.YArchitechIcon;
			this.airTerminalPara = airTerminalPara;
			this.InitParam();
		}

		private void InitParam()
		{
			if (this.airTerminalPara != null)
			{
				if (!string.IsNullOrEmpty(this.airTerminalPara.TerminalWidth.ToString()) && this.airTerminalPara.TerminalWidth > 0.0001)
				{
					this.label1.Text = "风口宽度(mm):";
					this.hytbTerminalWidth.Text = this.airTerminalPara.TerminalWidth.ToString();
				}
				if (!string.IsNullOrEmpty(this.airTerminalPara.TerminalHeight.ToString()) && this.airTerminalPara.TerminalHeight > 0.0001)
				{
					this.label2.Enabled = true;
					this.hytbTerminalHeight.Enabled = true;
					this.hytbTerminalHeight.Text = this.airTerminalPara.TerminalHeight.ToString();
				}
				if (!string.IsNullOrEmpty(this.airTerminalPara.TerminalDiameter.ToString()) && this.airTerminalPara.TerminalDiameter > 0.0001)
				{
					this.label1.Text = "风口直径(mm):";
					this.label2.Enabled = false;
					this.hytbTerminalHeight.Enabled = false;
					this.hytbTerminalWidth.Text = this.airTerminalPara.TerminalDiameter.ToString();
				}
				this.hytbTerminalThick.Text = this.airTerminalPara.TerminalThick.ToString();
				if (!string.IsNullOrEmpty(this.airTerminalPara.ThroatWidth.ToString()) && this.airTerminalPara.ThroatWidth > 0.0001)
				{
					this.label4.Text = "喉口宽度(mm):";
					this.hytbThroatWidth.Text = this.airTerminalPara.ThroatWidth.ToString();
				}
				if (!string.IsNullOrEmpty(this.airTerminalPara.ThroatHeight.ToString()) && this.airTerminalPara.ThroatHeight > 0.0001)
				{
					this.label5.Enabled = true;
					this.hytbThroatHeight.Enabled = true;
					this.hytbThroatHeight.Text = this.airTerminalPara.ThroatHeight.ToString();
				}
				if (!string.IsNullOrEmpty(this.airTerminalPara.ThroatDiameter.ToString()) && this.airTerminalPara.ThroatDiameter > 0.0001)
				{
					this.label4.Text = "喉口直径(mm):";
					this.label5.Enabled = false;
					this.hytbThroatHeight.Enabled = false;
					this.hytbThroatWidth.Text = this.airTerminalPara.ThroatDiameter.ToString();
				}
				this.hytbLoss.Text = this.airTerminalPara.Drop.ToString();
			}
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			this.airTerminalPara.Drop = double.Parse(this.hytbLoss.Text.Trim());
			if (this.label2.Enabled)
			{
				this.airTerminalPara.TerminalWidth = double.Parse(this.hytbTerminalWidth.Text.Trim());
				this.airTerminalPara.TerminalHeight = double.Parse(this.hytbTerminalHeight.Text.Trim());
			}
			else
			{
				this.airTerminalPara.TerminalDiameter = double.Parse(this.hytbTerminalWidth.Text.Trim());
			}
			this.airTerminalPara.TerminalThick = double.Parse(this.hytbTerminalThick.Text.Trim());
			if (this.label5.Enabled)
			{
				this.airTerminalPara.ThroatWidth = double.Parse(this.hytbThroatWidth.Text.Trim());
				this.airTerminalPara.ThroatHeight = double.Parse(this.hytbThroatHeight.Text.Trim());
			}
			else
			{
				this.airTerminalPara.ThroatDiameter = double.Parse(this.hytbThroatWidth.Text.Trim());
			}
			if (!this.checkParaValue())
			{
				return;
			}
			base.DialogResult = DialogResult.OK;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void hytb_TextChanged(object sender, EventArgs e)
		{
			if (!(sender is YJKTextBox))
			{
				return;
			}
			YJKTextBox hytextBox = sender as YJKTextBox;
			if (double.Parse(hytextBox.Text.Trim()) == 0.0)
			{
				YJKMessageBox.Show("数值不能是“0”，请重设！");
				string name = hytextBox.Name;
				if (!(name == "hytbTerminalWidth"))
				{
					if (name == "hytbTerminalHeight")
					{
						this.hytbTerminalHeight.Text = this.airTerminalPara.TerminalHeight.ToString();
						return;
					}
					if (name == "hytbTerminalThick")
					{
						this.hytbTerminalThick.Text = this.airTerminalPara.TerminalThick.ToString();
						return;
					}
					if (!(name == "hytbThroatWidth"))
					{
						if (name == "hytbThroatHeight")
						{
							this.hytbThroatHeight.Text = this.airTerminalPara.ThroatHeight.ToString();
							return;
						}
						if (!(name == "hytbLoss"))
						{
							return;
						}
						this.hytbLoss.Text = this.airTerminalPara.Drop.ToString();
					}
					else
					{
						if (this.airTerminalPara.ThroatDiameter > 0.001)
						{
							this.hytbThroatWidth.Text = this.airTerminalPara.ThroatDiameter.ToString();
							return;
						}
						this.hytbThroatWidth.Text = this.airTerminalPara.ThroatWidth.ToString();
						return;
					}
				}
				else
				{
					if (this.airTerminalPara.TerminalDiameter > 0.001)
					{
						this.hytbTerminalWidth.Text = this.airTerminalPara.TerminalDiameter.ToString();
						return;
					}
					this.hytbTerminalWidth.Text = this.airTerminalPara.TerminalWidth.ToString();
					return;
				}
			}
		}

		private bool checkParaValue()
		{
			if (this.label2.Enabled && this.label5.Enabled)
			{
				if (this.airTerminalPara.TerminalWidth <= this.airTerminalPara.ThroatWidth)
				{
					YJKMessageBox.Show("喉部宽度不得大于风口宽度，请\n重新设置喉部宽度值！");
					return false;
				}
				if (this.airTerminalPara.TerminalHeight <= this.airTerminalPara.ThroatHeight)
				{
					YJKMessageBox.Show("喉部高度不得大于风口高度，请\n重新设置喉部高度值！");
					return false;
				}
			}
			else if (this.airTerminalPara.TerminalDiameter <= this.airTerminalPara.ThroatDiameter)
			{
				YJKMessageBox.Show("喉部直径不得大于风口直径，请\n重新设置喉部直径！");
				return false;
			}
			return true;
		}

		public AirTerminalParamNew airTerminalPara;
	}
}
